package com.mobileseed.weather.data;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import android.util.Log;

public class WeatherXMLHandler extends DefaultHandler{
	private static final String HANDLE = "WeatherXMLHandler";
	private Weather weather;
	private int forecastConditionsNo = 0;
	private boolean dealWithCurrentConditions;
	private boolean dealWithForecastInfomation;
	private String FORECAST_CONDITIONS_TAG = "forecast_conditions";
	private String FORECAST_INFOMATION_TAG = "forecast_information";
	private Object CURRENT_CONDITIONS_TAG = "current_conditions";
	
	
	public WeatherXMLHandler() {
		super();
		// TODO Auto-generated constructor stub
	}

	public WeatherXMLHandler(Weather weather) {
		super();
		this.weather = weather;
	}
	
	@Override
	public void startDocument() throws SAXException {
		// TODO Auto-generated method stub
		Log.i(HANDLE, "startDocument");
		super.startDocument();
	}
	
	@Override
	public void endDocument() throws SAXException {
		// TODO Auto-generated method stub
		Log.i(HANDLE, "endDocument");
		super.endDocument();
	}
	
	@Override
	public void endElement(String uri, String localName, String qName)
			throws SAXException {
		if(qName.equals(FORECAST_INFOMATION_TAG)){
			dealWithForecastInfomation = false;
		}
		if(qName.equals(CURRENT_CONDITIONS_TAG )){
			dealWithCurrentConditions = false;
		}
		// TODO Auto-generated method stub
		super.endElement(uri, localName, qName);
	}
	
	@Override
	public void startElement(String uri, String localName, String qName,
			Attributes attributes) throws SAXException {		
		Log.i(HANDLE, qName);
		//处理城市信息
		if (qName.equals(FORECAST_INFOMATION_TAG)) {
			dealWithForecastInfomation = true;
		}
		if (dealWithForecastInfomation) {
			if (qName.equals("city")) {
				weather.getInformation().setCity(attributes.getValue("data"));
			}
			if (qName.equals("postal_code")) {
				weather.getInformation().setPostalCode(attributes.getValue("data"));
			}
			if (qName.equals("latitude_e6")) {
				weather.getInformation().setLatitude(attributes.getValue("data"));
			}
			if (qName.equals("longitude_e6")) {
				weather.getInformation().setLongitude(attributes.getValue("data"));
			}
			if (qName.equals("current_date_time")) {
				weather.getInformation().setCurrentDateTime(attributes.getValue("data"));
			}
			if (qName.equals("unit_system")) {
				weather.getInformation().setUnitSystem(attributes.getValue("data"));
			}
			if (qName.equals("forecast_date")) {
				weather.getInformation().setForecastDate(attributes.getValue("data"));
			}
			
		}
		//处理当前天气		
		if (qName.equals(CURRENT_CONDITIONS_TAG)) {
			dealWithCurrentConditions = true;
		}
		if (dealWithCurrentConditions) {
			if (qName.equals("condition")) {
				weather.getCurrentConditions().setCondition(attributes.getValue("data"));
			}
			if (qName.equals("temp_f")) {
				weather.getCurrentConditions().setTempF(attributes.getValue("data"));				
			}
			if (qName.equals("temp_c")) {
				weather.getCurrentConditions().setTempC(attributes.getValue("data"));				
			}
			if (qName.equals("humidity")){
				weather.getCurrentConditions().setHumidity(attributes.getValue("data"));
			}
			if (qName.equals("icon")){
				weather.getCurrentConditions().setIcon(attributes.getValue("data"));
			}
			if (qName.equals("wind_condition")){
				weather.getCurrentConditions().setWindCondition(attributes.getValue("data"));
			}
		}
		
		//处理未来四天的天气
		if (qName.equals(FORECAST_CONDITIONS_TAG)) {
			forecastConditionsNo++; 
		}
		if (forecastConditionsNo > 0) {
			ForecastConditions[] conditions = weather.getConditions();
			if (qName.equals("day_of_week")) {				
				conditions[forecastConditionsNo - 1].setDayOfWeek(attributes.getValue("data"));
			}			
			if (qName.equals("low")) {				
				conditions[forecastConditionsNo - 1].setLow(attributes.getValue("data"));
			}
			if (qName.equals("high")) {				
				conditions[forecastConditionsNo - 1].setHigh(attributes.getValue("data"));
			}
			if (qName.equals("icon")) {				
				conditions[forecastConditionsNo - 1].setIcon(attributes.getValue("data"));
			}
			if (qName.equals("condition")) {				
				conditions[forecastConditionsNo - 1].setCondition(attributes.getValue("data"));
			}
		}
		
		// TODO Auto-generated method stub
		super.startElement(uri, localName, qName, attributes);
	}
	
	@Override
	public void characters(char[] ch, int start, int length)
			throws SAXException {
		// TODO Auto-generated method stub
		super.characters(ch, start, length);
	}	
}
